var ue = UE.getEditor('editor');

$(function() {
    upload();
});



$('input[type=submit]').click(function() {
    var ed = (UE.getEditor('editor').getContent());
    // return false;
    var gouxuan = $('input[type=checkbox]').is(':checked');
    if(!gouxuan){
        $('#wrong').html('必须勾选同意按钮才可以发布文章')
        return false;
    }
    if ($('input[name=title]').val() == ""){
        $('#wrong').html('文章标题不能为空');
        $('input[name=title]').focus();
        return false;
    }

    if(ed ==""){
        $('#wrong').html('文章内容不能为空');
        UE.getEditor('editor').focus();
        return false;
    }

})
// 切片添加
$('.tianjia').click(function() {
    var index = $("#form1").children(".qiepian:last-child").data('index');
    index = index ? (index+1) : 0;
    var str = " <div class='fatieandedit-list qiepian' data-index='"+index+"'>" +
        "<div class='fatie-edit-left'><label>切片：</label></div>" +
        "<div class='fatie-edit-right'>" +
        "<input type='text' name='file["+index+"][name]' placeholder='切片名称' style='margin-right:4px;'>" +
        "<input type='hidden' name='file["+index+"][rename]' class='file-rename'>\n" +
        "<input type='hidden' name='file["+index+"][file]' class='file-file'>\n" +
        "<label for='file-" + index + "' class='filebtn' style='margin-right:4px;'>上传切片</label>" +
        "<input type='file' name='file["+index+"][path]' id='file-" + index + "' onchange='showPreview(this)' >" +
        "<span class='file-info'></span><a href='javascript:;' class='deleteImg' >删除切片</a>" +
        "</div></div>";
    $('#form1').append(str);
    upload();
})

function upload() {
    //触发的id
    var obj = $('.qiepian');
    for(var i = 0 ; i < obj.length; i++){
        (function(i){
            var object = $(obj.eq(i));
            var child = object.find("input[type=file]")[0];
            var self = child.id;
            //实例化一个plupload上传对象
            var uploader = new plupload.Uploader({
                runtimes: 'html5,flash,silverlight,html4',
                flash_swf_url: '__CDN__/assets/libs/plupload/js/Moxie.swf',
                silverlight_xap_url: '__CDN__/assets/libs/plupload/js/Moxie.xap',
                browse_button: self, //触发文件选择对话框的按钮，为那个元素id
                url: '/index/ajax/uploads',//服务器端的上传页面地址
                chunk_size: '100bm',
                max_retries: 5,
                filters: {
                    // mime_types: [{ title : "Zip files", extensions : "zip" }],
                    max_file_size: '10000mb',
                    prevent_duplicates: true,
                }
            });
            var info = $(object.find(".file-info")[0]);
            var oldname = $(object.find(".file-rename")[0]);
            var file = $(object.find(".file-file")[0]);

            //在实例对象上调用init()方法进行初始化
            uploader.init();
            //绑定各种事件，并在事件监听函数中做你想做的事
            uploader.bind('FilesAdded', function (uploader, files) {
                uploader.start();
            });
            uploader.bind('FileUploaded', function (uploader, files, data) {
                var res = JSON.parse(data.response);
                info.text(res.oldName);
                oldname.val(res.oldName);
                file.val(res.filePaht);
            });
            uploader.bind('UploadProgress', function (uploader, files) {
                info.text("上传中 "+files.percent + "%");
            });
            uploader.bind('Error', function (uploader, err) {
                info.text("上传失败！");
            });

            $("body").on('click', '.deleteImg', function() {
                uploader.stop();
                var images = $(this).siblings('input[type=file]');
                images.parent().parent().remove();
            });
        })(i)
    }
}

function showPreview(source) {
    var arrs = $(source).val().split('\\');
    var filename = arrs[arrs.length - 1];
    $(source).next().html(filename);
    var file = source.files[0];
    var total = file.size;
    if (window.FileReader) {
        var fr = new FileReader();
        fr.onprogress = function (e) { // $(".progress1").show();
            $("#Progress").html('文件上传中');
        }
        //$("#Progress").html((e.loaded/total)*100) };
        fr.onabort = function () {
            alert("文件上传中断,请重试")
        };
        fr.onerror = function () {
            alert("文件上传出错，请重试")
        };
        fr.onload = function () {
            $(".progress1").hide();
            //alert("文件上传成功")
        };
        fr.readAsDataURL(file);
    }
}